package io.plus.user_interface.io.common.utils;

import cn.hutool.core.bean.BeanUtil;
import io.plus.interactive.AsynchronousSocketChannelContext;
import io.plus.user_interface.io.common.packet.Command;
import io.plus.user_interface.io.common.packet.Group;
import io.plus.user_interface.io.common.packet.ResponseMessage;
import io.plus.user_interface.io.common.packet.User;
import io.plus.user_interface.io.common.protocol.IPacketConverter;
import io.plus.user_interface.io.common.protocol.PacketConverter;
import io.plus.user_interface.io.common.protocol.PacketImpl;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ImKit {
    public static PacketImpl convertResponsepPacket(PacketImpl packet, Command command, AsynchronousSocketChannelContext asynchronousSocketChannelContext) {
        PacketImpl respPacket = convertResponsepPacket(packet.getBody(), command, asynchronousSocketChannelContext);
        return respPacket;
    }

    public static PacketImpl convertResponsepPacket(byte[] body, Command command, AsynchronousSocketChannelContext asynchronousSocketChannelContext) {
        IPacketConverter packetConverter = new PacketConverter();
        PacketImpl respPacket = packetConverter.converteResponsepPacket(body, command, asynchronousSocketChannelContext);

        return respPacket;
    }


    /**
     * 复制分组或者群组，不包括users;
     *
     * @param source
     * @return
     */
    public static Group copyGroupWithoutUsers(Group source) {
        if (source == null) {
            return null;
        }
        Group group = new Group();
        BeanUtil.copyProperties(source, group, "users");
        return group;
    }

    /**
     * 复制用户信息不包括friends、groups下的users信息;
     *
     * @param source
     * @return
     */
    public static User copyUserWithoutFriendsGroups(User source) {
        if (source == null) {
            return null;
        }
        User user = new User();
        BeanUtil.copyProperties(source, user, "friends", "groupIdSet");
        return user;
    }

    /**
     * 复制用户信息不包括friends、groups下的users信息;
     */
    public static User copyUserWithoutUsers(User source) {
        if (source == null) {
            return source;
        }
        User user = new User();
        BeanUtil.copyProperties(source, user, "friends", "groupIdSet");
        List<Group> friends = source.getFriends();
        if (friends != null && !friends.isEmpty()) {
            List<Group> newFriends = new ArrayList<Group>();
            for (Group friend : friends) {
                Group newFriend = new Group();
                BeanUtil.copyProperties(friend, newFriend);
                newFriend.setUsers(null);
                newFriends.add(newFriend);
            }
            user.setFriends(newFriends);
        }
        List<Group> groups = source.getGroups();
        if (groups != null && !groups.isEmpty()) {
            List<Group> newGroups = new ArrayList<Group>();
            for (Group group : groups) {
                Group newGroup = new Group();
                //BeanUtil.copyProperties(group, newGroup);
                newGroup.setUsers(null);
                newGroups.add(newGroup);
            }
            user.setGroups(newGroups);
        }
        return user;
    }


    public static PacketImpl convertResponsepPacket(ResponseMessage respBody, AsynchronousSocketChannelContext asynchronousSocketChannelContext) {
        return null;
    }
}
